package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {

    private TeacherService teacherService = new TeacherService();
    Scanner sc = new Scanner(System.in);

    public void start() {

        TeacherLoop:
        while (true) {

            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            int choice = sc.nextInt();

            switch (choice) {
                case 1:
                    //System.out.println("添加");
                    addTeacher();
                    break;
                case 2:
                    //System.out.println("删除");
                    delTeacher();
                    break;
                case 3:
                    //System.out.println("修改");
                    changeTeacher();
                    break;
                case 4:
                    //System.out.println("查看");
                    getAllTeacher();
                    break;
                case 5:
                    System.out.println("感谢您的使用");
                    break TeacherLoop;
                default:
                    System.out.println("您输入有误,请重新输入:");
                    break;

            }

        }
    }

    private void changeTeacher() {

        Teacher[] allTeacher = noTeachers();
        if (allTeacher == null) return;

        String id = existOrNot();
        Teacher tea = buildTeacher(id);
        teacherService.changeTeacher(id, tea);
        System.out.println("修改成功");

    }

    private void delTeacher() {

        Teacher[] allTeacher = noTeachers();
        if (allTeacher == null) return;

        String id = existOrNot();
        teacherService.delTeacher(id);
        System.out.println("删除成功");

    }

    private void getAllTeacher() {

        Teacher[] allTeacher = noTeachers();
        if (allTeacher == null) return;

        System.out.println("id\t\tname\tage\tbrithday");

        for (int i = 0; i < allTeacher.length; i++) {
            if (allTeacher[i] != null) {
                System.out.println(allTeacher[i].getId() + "\t" + allTeacher[i].getName() + "\t\t" + allTeacher[i].getAge() + "\t" + allTeacher[i].getBrithday() + "\t");
            }
        }
    }

    public Teacher[] noTeachers() {
        Teacher[] allTeacher = teacherService.getAllTeacher();

        if (allTeacher == null) {

            System.out.println("查无信息,请添加老师后重试");
            return null;

        }
        return allTeacher;
    }

    public void addTeacher() {

        String id;
        while (true) {

            System.out.println("请输入老师id:");
            id = sc.next();

            boolean exists = teacherService.isExists(id);
            if (exists) {
                System.out.println("学号已被占用,请重新输入:");
            } else {
                break;
            }
        }
        Teacher tea = buildTeacher(id);

        boolean result = teacherService.addTeacher(tea);

        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败,老师id已满");
        }
    }

    public Teacher buildTeacher(String id) {
        System.out.println("请输入老师姓名:");
        String name = sc.next();
        System.out.println("请输入老师年龄:");
        String age = sc.next();
        System.out.println("请输入老师生日:");
        String brithday = sc.next();

        Teacher tea = new Teacher(id, name, age, brithday);
        return tea;
    }

    public String existOrNot() {

        String id;
        while (true) {

            System.out.println("请输入老师id:");
            id = sc.next();

            boolean exists = teacherService.isExists(id);
            if (!exists) {
                System.out.println("您输入的id不存在,请重新输入:");
            } else {
                break;
            }
        }
        return id;
    }
}
